package leetcode.median;

/**
 * ClassName: $695_MaxAreaOfIsland
 * Description:
 * date: 2022-02-17 17:06
 *
 * @author liyifan
 */
public class $695_MaxAreaOfIsland {
    int rows;
    int cols;
    public int maxAreaOfIsland(int[][] grid) {
        rows = grid.length;
        cols = grid[0].length;
        int res = 0;
        for (int i = 0;i < rows;i++){
            for (int j = 0;j < cols;j++) {
                if (grid[i][j] == 1) {
                    res = Math.max(res, area(grid, i, j));
                }
            }
        }
        return res;
    }

    public int area(int[][] grid, int r, int c){
        if (!isArea(grid, r, c))
            return 0;
        if (grid[r][c] != 1)
            return 0;
        grid[r][c] = 2;
        return 1 + area(grid, r - 1, c) + area(grid, r + 1, c) + area(grid, r, c- 1) + area(grid, r, c + 1);
    }

    public boolean isArea(int[][] grid, int r, int c){
        return r >= 0 && r < rows && c >=0 && c < cols;
    }
}
